package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class UpDownListener {
    @Autowired
    private IApArticleService articleService;
    @KafkaListener(topics = {"${topic.upDownTopic}"})
    public void handleMenssage(ConsumerRecord<String, String> record) {
        String articleId=record.key();
        String isDown=record.value();
        LambdaUpdateWrapper<ApArticle> update=new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId,Long.parseLong(articleId));
        update.eq(ApArticle::getIsDown, isDown.equals("true") ? 1 : 0);
        articleService.update(update);
    }
}
